import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:nyh_motorcade_app/widgets/custom_text_field.dart';
import 'package:nyh_motorcade_app/widgets/left_right_text.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/precision_limit_formatter.dart';
import 'company_manage_controller.dart';

class CompanyManagePage extends StatelessWidget {
  final controller = Get.put(CompanyManageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "公司管理"),
      body: GetBuilder<CompanyManageController>(
        builder: (controller) {
          return MultiStateWidget(
            child: Column(
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    physics: BouncingScrollPhysics(),
                    child: Column(
                      children: [
                        SizedBox(
                          height: 12.w,
                        ),
                        LeftRightText(
                          textLeft: "公司编号",
                          textRight: controller.companyInfoEntity?.code ?? "",
                        ),
                        Divider(
                          color: Colours.color_divider,
                          height: 1.w,
                          thickness: 1.w,
                        ),
                        LeftRightText(
                          textLeft: "公司名称",
                          textRight: controller.companyInfoEntity?.name ?? "",
                        ),
                        Divider(
                          color: Colours.color_divider,
                          height: 1.w,
                          thickness: 1.w,
                        ),
                        LeftRightText(
                          textLeft: "结算方式",
                          textRight: controller.companyInfoEntity?.settlementMethodName ?? "",
                        ),
                        SizedBox(
                          height: 12.w,
                        ),
                        LeftRightText(
                          textLeft: "总余额(元)",
                          textRight: FormatUtils.formatMoney(controller.totalAmount),
                        ),
                        Divider(
                          color: Colours.color_divider,
                          height: 1.w,
                          thickness: 1.w,
                        ),
                        LeftRightText(
                          textLeft: "公司账户余额(元)",
                          textRight: FormatUtils.formatMoney(controller.companyAmount),
                        ),
                        Divider(
                          color: Colours.color_divider,
                          height: 1.w,
                          thickness: 1.w,
                        ),
                        LeftRightText(
                          textLeft: "车队账户余额(元)",
                          textRight: FormatUtils.formatMoney(controller.motorcadeAmount),
                        ),
                        Divider(
                          color: Colours.color_divider,
                          height: 1.w,
                          thickness: 1.w,
                        ),
                        LeftRightText(
                          textLeft: "卡账户余额(元)",
                          textRight: FormatUtils.formatMoney(controller.cardAmount),
                        ),
                        SizedBox(
                          height: 12.w,
                        ),
                        Container(
                          padding: EdgeInsets.only(left: 16.w, right: 14.w),
                          color: Colours.color_white,
                          height: 53.w,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                "余额提醒",
                                style: TextStyle(
                                  color: Colours.color_text_theme,
                                  fontSize: 14.sp,
                                ),
                              ),
                              CupertinoSwitch(
                                value: controller.isBalanceReminder,
                                activeColor: Colours.color_theme,
                                trackColor: Colours.color_cfcfd5,
                                onChanged: (value) {
                                  controller.changeBalanceReminderState(value);
                                },
                              ),
                            ],
                          ),
                        ),
                        Visibility(
                          visible: controller.isBalanceReminder,
                          child: Divider(
                            color: Colours.color_divider,
                            height: 1.w,
                            thickness: 1.w,
                          ),
                        ),
                        Visibility(
                          visible: controller.isBalanceReminder,
                          child: Container(
                            padding: EdgeInsets.only(left: 16.w, right: 11.w),
                            color: Colours.color_white,
                            height: 53.w,
                            child: Row(
                              children: [
                                Text(
                                  "提醒额度",
                                  style: TextStyle(
                                    color: Colours.color_text_theme,
                                    fontSize: 14.sp,
                                  ),
                                ),
                                SizedBox(
                                  width: 16.w,
                                ),
                                Expanded(
                                  child: CustomTextField(
                                    "请输入提醒额度",
                                    controller: controller.quotaController,
                                    focusNode: controller.quotaFocus,
                                    hintStyle: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
                                    textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                                    textAlign: TextAlign.right,
                                    contentPadding: EdgeInsets.symmetric(vertical: 15),
                                    keyboardType: TextInputType.numberWithOptions(decimal: true),
                                    inputFormatters: [
                                      LengthLimitingTextInputFormatter(10),
                                      FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
                                      PrecisionLimitFormatter(2),
                                    ],
                                    onChanged: (value) {
                                      controller.setQuota(value);
                                    },
                                  ),
                                ),
                                SizedBox(
                                  width: 11.w,
                                ),
                                TextButton(
                                  onPressed: () {
                                    controller.setBalanceReminder();
                                  },
                                  child: Text(
                                    "设置",
                                    style: TextStyle(
                                      color: controller.quota > 0.0 ? Colours.color_34c759 : Colours.color_b1e3c2,
                                      fontSize: 14.sp,
                                    ),
                                  ),
                                  style: ButtonStyle(
                                    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                    minimumSize: MaterialStateProperty.all(Size(0, 0)),
                                    padding: MaterialStateProperty.all(EdgeInsets.all(5.w)),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                        Visibility(
                          visible: controller.isBalanceReminder,
                          child: Container(
                            alignment: Alignment.centerRight,
                            padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                            child: Text(
                              "总余额低于设置金额时提醒",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 12.sp,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Container(
                  color: Colours.color_white,
                  padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 16.w),
                  child: Row(
                    children: [
                      // Expanded(
                      //   child: TextButton(
                      //     onPressed: () {},
                      //     child: Text(
                      //       "分公司及车队",
                      //       style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
                      //       maxLines: 1,
                      //       overflow: TextOverflow.ellipsis,
                      //     ),
                      //     style: ButtonStyle(
                      //       backgroundColor: MaterialStateProperty.all(Colours.color_white),
                      //       shape: MaterialStateProperty.all(
                      //         RoundedRectangleBorder(
                      //           borderRadius: BorderRadius.circular(5.w),
                      //           side: BorderSide(width: 1.w, color: Colours.color_cfcfd5),
                      //         ),
                      //       ),
                      //       minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(
                      //   width: 16.w,
                      // ),
                      Expanded(
                        child: TextButton(
                          onPressed: () {
                            controller.goToCashFlowPage();
                          },
                          child: Text(
                            "公司资金流水",
                            style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all(Colours.color_white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.w),
                                side: BorderSide(width: 1.w, color: Colours.color_cfcfd5),
                              ),
                            ),
                            minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 16.w,
                      ),
                      Expanded(
                        child: TextButton(
                          onPressed: () {
                            controller.goToCompanySettlementPage();
                          },
                          child: Text(
                            "公司结算",
                            style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                          style: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all(Colours.color_white),
                            shape: MaterialStateProperty.all(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.w),
                                side: BorderSide(width: 1.w, color: Colours.color_theme),
                              ),
                            ),
                            minimumSize: MaterialStateProperty.all(Size(double.infinity, 40.w)),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            state: controller.multiState,
            onRetry: () {
              controller.onRetry();
            },
          );
        },
      ),
    );
  }
}
